! Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
! See https://llvm.org/LICENSE.txt for license information.
! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
!
! test ACOS intrinsic with quad-precision arguments

program test
  implicit none
  integer, parameter :: n = 16
  integer, parameter :: m = n * 4
  integer, parameter :: k = 16
  real(k), parameter :: pi_q = 3.1415926535897932384626433832795028841_k
  real(k), parameter :: q_tol = 5e-33_k
  real(k), parameter :: radians(n) = (/ &
    1.00000000000000000000000000000000000_k, &
    0.866025403784438646763723170752936161_k, &
    0.707106781186547524400844362104849088_k, &
    0.499999999999999999999999999999999952_k, &
    0.433590506506189051239852201302167613E-34_k, &
    -0.500000000000000000000000000000000096_k, &
    -0.707106781186547524400844362104848992_k, &
    -0.866025403784438646763723170752936257_k, &
    -1.00000000000000000000000000000000000_k, &
    -0.866025403784438646763723170752936161_k, &
    -0.707106781186547524400844362104849088_k, &
    -0.499999999999999999999999999999999856_k, &
    -0.130077151951856715371955660390650284E-33_k, &
    0.500000000000000000000000000000000289_k, &
    0.707106781186547524400844362104848896_k, &
    0.866025403784438646763723170752936257_k &
  /)
  integer :: i
  real(k), dimension(n) :: arg
  real(k) :: rst(n) = acos(radians)
  real(k), parameter :: rst_p(n) = acos(radians)
  real(k) :: result(m), expect(m)

  expect(1:n) = (/ &
    0.00000000000000000000000000000000000_k, &
   0.523598775598298873077107230546583832_k, &
   0.785398163397448309615660845819875699_k, &
    1.04719755119659774615421446109316766_k, &
    1.57079632679489661923132169163975140_k, &
    2.09439510239319549230842892218633533_k, &
    2.35619449019234492884698253745962710_k, &
    2.61799387799149436538553615273291925_k, &
    3.14159265358979323846264338327950280_k, &
    2.61799387799149436538553615273291925_k, &
    2.35619449019234492884698253745962710_k, &
    2.09439510239319549230842892218633494_k, &
    1.57079632679489661923132169163975159_k, &
    1.04719755119659774615421446109316728_k, &
   0.785398163397448309615660845819875892_k, &
   0.523598775598298873077107230546583639_k  &
  /)

  expect(n+1:2*n) = expect(1:n)
  expect(2*n+1:3*n) = expect(1:n)
  expect(3*n+1:4*n) = expect(1:n)

  arg = radians
  result(1:n) = acos(arg)
  result(n+1:2*n) = acos(radians)
  result(2*n+1:3*n) = rst
  result(3*n+1:4*n) = rst_p
  do i = 1, m
    if (expect(i) .eq. 0.0_k) then
      if (result(i) .ne. expect(i)) STOP i
    else
      if (abs((result(i) - expect(i)) / expect(i)) .gt. q_tol) STOP i
    endif
  enddo

  print *, 'PASS'

end
